//
//  MYXRedListController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/21.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我的钱包明细

#import "MYXRedListController.h"
#import "MYXRedListCell.h"
#import "MYCmymoneylogViewModel.h"
#import "MYCmymoneylogModel.h"


static NSString *const MYXRedListCellIdentifier = @"MYXRedListCell";
@interface MYXRedListController ()<
UITableViewDataSource,
UITableViewDelegate,
UIGestureRecognizerDelegate,
UIActionSheetDelegate,
UINavigationControllerDelegate,
UIAlertViewDelegate>{
    MYCmymoneylogViewModel *MYCmymoneylogViewM;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property(nonatomic, assign) int pageStart;
@property(nonatomic, assign) int type;//判断是否刷新
@property(nonatomic, strong) MYXHttpManager *MYXHttpMan;

@end

@implementation MYXRedListController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (MYCmymoneylogViewM == nil) {
        MYCmymoneylogViewM = [[MYCmymoneylogViewModel alloc] init];
    }

    self.pageStart=1;
    [self createUI];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UI

- (void)createUI
{
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MYXRedListCellIdentifier bundle:nil] forCellReuseIdentifier:MYXRedListCellIdentifier];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        WS(weakSelf)
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf loadNewData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMorewData];
        }];
    }
    return _tableView;
}
#pragma mark - request

- (void)loadNewData
{
    [self.tableView.mj_header beginRefreshing];
    self.pageStart = 1;
    self.type =0;
    [self Cmymoneylog:self.pageStart];
    
}
- (void)loadMorewData
{
    if (self.tableView.mj_header.isRefreshing) {
        return;
    }
    self.pageStart ++;
    self.type =1;
    [self Cmymoneylog:self.pageStart];
}

#pragma mark - tableview datasource and delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return MYCmymoneylogViewM.CmymoneylogArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYXRedListCell *MYXMyBoossCe = [tableView dequeueReusableCellWithIdentifier:MYXRedListCellIdentifier forIndexPath:indexPath];
    MYXMyBoossCe.selectionStyle = UITableViewCellSelectionStyleNone;
    MYCmymoneylogModel *MYCmymoneylogMo=MYCmymoneylogViewM.CmymoneylogArray[indexPath.row];
    MYXMyBoossCe.MYCmymoneylogMo =MYCmymoneylogMo;
    return MYXMyBoossCe;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 51;
}

#pragma mark -  钱来钱往接口


-(void)Cmymoneylog:(int)page{
    if (self.type==0) {
        [MYCmymoneylogViewM.CmymoneylogArray removeAllObjects];
    }
    [MYCmymoneylogViewM  loadCmymoneylog:page WithResult:^(BOOL isSucess) {
        if (isSucess) {
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
       
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
